#include "stdafx.h"
#include "XepdtRegistration.h"

CXepdtRegistration::CXepdtRegistration()
{
	m_regResult = RegistrationSuccess;
}

CXepdtRegistration::~CXepdtRegistration(void)
{
}

RegistrationResult CXepdtRegistration::registerAccount(std::string& strServer, std::string& username, std::string& password)
{
	user = username;
	pwd = password;
	client = new Client( strServer );
	client->disableRoster();
	client->registerConnectionListener( this );

	m_reg = new Registration( client );
	m_reg->registerRegistrationHandler( this );

	client->connect();

	delete( m_reg );
	delete( client );

	return m_regResult;
}

void CXepdtRegistration::onConnect()
{
	m_reg->fetchRegistrationFields();
}

void CXepdtRegistration::onDisconnect( ConnectionError e ) 
{
}

bool CXepdtRegistration::onTLSConnect( const CertInfo& info )
{
	return true;
}

void CXepdtRegistration::handleRegistrationFields( const JID& /*from*/, int fields, std::string instructions )
{
	RegistrationFields vals;
	vals.username = user;
	vals.password = pwd;
	m_reg->createAccount( fields, vals );
}

void CXepdtRegistration::handleRegistrationResult( const JID& /*from*/, RegistrationResult result )
{
	m_regResult = result;
	client->disconnect();
}

void CXepdtRegistration::handleAlreadyRegistered( const JID& /*from*/ )
{
	m_regResult = RegistrationConflict;
}

void CXepdtRegistration::handleDataForm( const JID& /*from*/, const DataForm& /*form*/ )
{
}

void CXepdtRegistration::handleOOB( const JID& /*from*/, const OOB& oob )
{
}
